﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Excel = Microsoft.Office.Interop.Excel;
using ExcelLibrary;
using ExcelLibrary.SpreadSheet;

using cecbClasses.Data_Adapters.Project;



//using Spire.Xls;


using cecbData;
using cecbClasses;
using cecbClasses.Data_Classes;
using cecbClasses.Data_Adapters;
using System.Data;
using System.Collections.ObjectModel;

namespace CECB.Matrix
{
    /// <summary>
    /// Interaction logic for SimpleMatrixFinalResult_Window.xaml
    /// </summary>
    public partial class SimpleMatrixFinalResult_Window : Window
    {

        string destFile1;


        Func_Projects ObjProjctsFunc = new Func_Projects();

        Excel.Application oXL;
        Excel._Workbook oWB;
        Excel._Worksheet oSheet;

        public SimpleMatrixFinalResult_Window()
        {
            InitializeComponent();
            dataContext = new DSSEntities();
        }

        DSSEntities dataContext;


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // get the project referance

            String pref = "PROJ1";
           
            string startColName, endColName;
            int startColNo, endColNo;


            oXL = new Excel.Application();
            oXL.Visible = true;

            //Get a new workbook.
            oWB = (Excel._Workbook)(oXL.Workbooks.Add());
            oSheet = (Excel._Worksheet)oWB.ActiveSheet;

            // MessageBox.Show(Convert.ToChar(65).ToString());

            DataAdapter da = new DataAdapter();
            List<Get_Leopold_Phase_Result> LstResult = ObjProjctsFunc.GetLeopoldPhases(pref);
            List<Get_Leopold_Activities_Result> lstActivities = ObjProjctsFunc.GetLeopoldActivities(pref);
            List<Get_Leopold_Impacts_Result> lstImpacts = ObjProjctsFunc.GetLeopolImpacts(pref);
            List<Get_Leopold_Matric_Result> lstMatrice = ObjProjctsFunc.GetLeopolMatrices(pref);
            List<Get_Leopold_Simple_Matrix_Value_Result_Result> lstSimple = ObjProjctsFunc.GetLeopolSimpleValues(pref);

            // ---- Header Row ---
            oSheet.Cells[3, 1] = "Category";
            oSheet.get_Range("A3").ColumnWidth = 15;
            oSheet.get_Range("A3").Borders.Weight = 1;
            oSheet.get_Range("A3").Borders.LineStyle = 6;


            oSheet.Cells[3, 2] = "Component";
            oSheet.get_Range("B3").ColumnWidth = 25;

            oSheet.Cells[3, 4] = "Project Impact";
            oSheet.get_Range("D3").ColumnWidth = 70;
            oSheet.get_Range("C3", "D3").Merge();

            oSheet.get_Range("A3", "D3").Font.Bold = true;
            oSheet.get_Range("A3", "D3").VerticalAlignment = VerticalAlignment.Center;

            int z = 5;
            startColNo = 69;
            startColName = Convert.ToChar(startColNo).ToString() + "2";

            // --- Phases Headings -----
            foreach (Get_Leopold_Phase_Result oRsts in LstResult)
            {
                endColNo = startColNo + (((int)oRsts.NoOfActivities) * 2) - 1;

                endColName = Convert.ToChar(endColNo).ToString() + "2";

                oSheet.Cells[2, z] = oRsts.actv_phase;

                oSheet.get_Range(startColName, endColName).Font.Bold = true;

                oSheet.get_Range(startColName, endColName).Merge();
                oSheet.get_Range(startColName, endColName).HorizontalAlignment = HorizontalAlignment.Center;
                oSheet.get_Range(startColName, endColName).Borders.Weight = 1;
                startColNo = endColNo + 1;
                startColName = Convert.ToChar(startColNo).ToString() + "2";

                z = z + (int)oRsts.NoOfActivities * 2;
                DrawCellBorders(2, z, 2, z, true, true, true, true, false);

            }

            //// --- Get Activities ---
            startColNo = 69;
            startColName = Convert.ToChar(startColNo).ToString() + "4";

            int startColNo2 = 69;
            string startColName2 = Convert.ToChar(startColNo2).ToString() + "3";
            int endColNo2;
            string endColName2;

            int ActNo = 1;
            z = 5;
            foreach (Get_Leopold_Activities_Result oRstAct in lstActivities)
            {
                endColNo2 = startColNo2 + 1;
                endColName2 = Convert.ToChar(endColNo2).ToString() + "3";

                endColNo = startColNo + 1;
                endColName = Convert.ToChar(endColNo).ToString() + "4";

                oSheet.Cells[3, z] = ActNo;
                oSheet.Cells[4, z] = oRstAct.actv_name;

                oSheet.get_Range(startColName, endColName).Font.Bold = true;
                oSheet.get_Range(startColName, endColName).Orientation = 90;
                oSheet.get_Range(startColName, endColName).RowHeight = 200;
                oSheet.get_Range(startColName, endColName).WrapText = true;

                // comment 1-----------still commented-----------------------

                // oSheet.get_Range(startColName, endColName).Merge();
                oSheet.get_Range(startColName, endColName).Borders.Weight = 1;
                // comment 2----------------------------------
                //oSheet.get_Range(startColName2, endColName2).Merge();
                oSheet.get_Range(startColName2, endColName2).Font.Bold = true;
                oSheet.get_Range(startColName2, endColName2).HorizontalAlignment = HorizontalAlignment.Center;
                oSheet.get_Range(startColName2, endColName2).Borders.Weight = 1;


                startColNo = startColNo + 2;
                startColName = Convert.ToChar(startColNo).ToString() + "4";

                startColNo2 = startColNo2 + 2;
                startColName2 = Convert.ToChar(startColNo2).ToString() + "3";
                z = z + 1;

                ActNo = ActNo + 1;
            }

            // --- Print Impacts , Magnitude, Imports
            // --- IMPACTS ---
            startColNo = 69;
            startColName = Convert.ToChar(startColNo).ToString() + "5";

            string srtCatName, endCatName, srtSubName, endSubName, srtImpColNoName, endImpColNoName, srtImpColName, endImpColName;

            int srtCatNo, endCatNo, srtSubNo;
            int LineNO = 1;
            int rowNo = 5;

            float GrandTotal = 0;

            string CatName = lstImpacts[0].cmpt_mainCat.ToString();
            oSheet.Cells[5, 1] = CatName;

            srtCatNo = 5;
            srtSubNo = 5;
            string SubName = lstImpacts[0].cmpt_name.ToString();
            oSheet.Cells[5, 2] = SubName;
            
            foreach (Get_Leopold_Impacts_Result oRstImpacts in lstImpacts)
            {
                z = 5;

                if (CatName != oRstImpacts.cmpt_mainCat)
                {
                    srtCatNo = rowNo;
                    CatName = oRstImpacts.cmpt_mainCat;
                    oSheet.Cells[rowNo, 1] = CatName;
                }

                if (SubName != oRstImpacts.cmpt_name)
                {
                    srtSubNo = rowNo;
                    SubName = oRstImpacts.cmpt_name;
                    oSheet.Cells[rowNo, 2] = SubName;
                }

                startColNo = 69;
                startColName = Convert.ToChar(startColNo).ToString() + rowNo;

                endColNo = startColNo + 1;
                endColName = Convert.ToChar(endColNo).ToString() + rowNo;

                oSheet.Cells[rowNo, 4] = oRstImpacts.impt_name;

                oSheet.Cells[rowNo, 3] = LineNO;
                srtImpColNoName = "C" + rowNo;
                endImpColNoName = "C" + (rowNo + 1);

                srtImpColName = "D" + rowNo;
                endImpColName = "D" + (rowNo + 1);
                // comment3 -----
                // oSheet.get_Range(srtImpColNoName, endImpColNoName).Merge();
                //comment 4-----
                //oSheet.get_Range(srtImpColName, endImpColName).Merge();


                //oSheet.get_Range("A" + srtCatNo, "A" + (rowNo + 1)).Merge();

                //oSheet.get_Range("B" + srtSubNo, "B" + (rowNo + 1)).Merge();

                float Total = 0;
                
                // --- MATRIX DATA ----
                foreach (Get_Leopold_Activities_Result oRstAct in lstActivities)
                {
                    endColNo = startColNo + 1;
                    endColName = Convert.ToChar(endColNo).ToString() + rowNo;

                    var filter = (from i in lstSimple
                                  where i.actv_reference == oRstAct.actv_reference && i.impt_reference == oRstImpacts.impt_reference
                                  select i).ToList();

                    if (filter.Count() > 0)
                    {
                        //oSheet.Cells[rowNo, z] = filter[0].mtrx_magnitude;
                        float svalue = 0;

                        try
                        {
                             svalue = (float)filter[0].mtrx_simpleValue;
                        }
                        catch (InvalidOperationException)
                        {
                            svalue = 0;
                        }
                        oSheet.Cells[rowNo, z] = svalue;

                        Total = Total + svalue;


                        //=============================================

                        //=============================================



                    }

                    oSheet.get_Range(startColName, endColName).ColumnWidth = 2.5;

                    // DrawCellBorders(rowNo, z + 1, rowNo, z + 1, true, false, false, true, true);
                    // DrawCellBorders(rowNo + 1, z, rowNo + 1, z, false, true, true, false, true);
                    // DrawCellBorders(rowNo + 1, z + 1, rowNo + 1, z + 1, false, true, false, true, false);

                    startColNo = endColNo + 1;
                    startColName = Convert.ToChar(startColNo).ToString() + rowNo;

                    z = z + 1;
                }

                oSheet.Cells[rowNo, z] = Total;
                

              //  oSheet.Cells[rowNo, z + 1] = (float)Total / MaxTotal;
              //  oSheet.Cells[rowNo, z + 2] = 42;
                endColName = Convert.ToChar(startColNo) + (rowNo + 1).ToString();
                oSheet.get_Range(startColName, endColName).Merge();


                GrandTotal = GrandTotal + Total;

                rowNo = rowNo + 2;
                LineNO = LineNO + 1;
            }

            oSheet.Cells[4, z] = "Total";
           
         //   oSheet.Cells[4, z + 2] = "Physical Impact";
         ///   oSheet.Cells[4, z + 3] = "Sociological Impact";
         //   oSheet.Cells[4, z + 4] = "Echological Impact";

            oSheet.Cells[rowNo, z] = GrandTotal;





            // -----------------------------------Calculate Weighted Avg------------------------------------------
            decimal WAGrandTotal = 0;
            decimal CatWiseWATotal = 0;
            int catStrLNo = 5;

            oSheet.Cells[4, z + 1] = "Weighted Average";

            for (int Lno = 5; Lno <= rowNo - 1; Lno = Lno+2)
            {

                var cell = (Excel.Range)oSheet.Cells[Lno, z];

                if (Convert.ToString(cell.Value2) != null)
                {

                    //oSheet.Cells[10] = (decimal)45 / (int)40;
                   // WAGrandTotal = WAGrandTotal + (decimal)GrandTotal / (int)cell.Value2;
                    WAGrandTotal = WAGrandTotal +  (decimal)cell.Value2 / (int)GrandTotal;
                    // -- Calculate Total Weighted Avg --


                    oSheet.Cells[catStrLNo, z + 1] = (decimal)cell.Value2 / (int)GrandTotal;

                    //if (((Convert.ToString(((Excel.Range)oSheet.Cells[Lno + 2, 1]).Value2)) != null) && (Lno != 5))
                    //{
                        catStrLNo = Lno + 2;
                      
                   // }
                }
            }
            oSheet.Cells[catStrLNo, z + 1] = WAGrandTotal;
          //  oSheet.Cells[rowNo, z + 1] = WAGrandTotal;


            oXL.Visible = true;
            oXL.UserControl = true;

        }
        ////////------------------------------------------------------------------------------------------------------------------------------------
        private void DrawCellBorders(int frmrowNo, int frmColNo, int TorowNo, int ToColNo, bool top, bool bottom, bool left, bool right, bool diagup)
        {
            Excel.Borders b = null;
            Excel.Borders fb = null;

            b = (Excel.Borders)(oSheet.Cells[frmrowNo, frmColNo] as Excel.Range).Borders;
            fb = (oSheet.Cells[frmrowNo, frmColNo] as Excel.Range).Borders;


            if (diagup == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp].Weight;
            }
            if (top == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Weight;
            }
            if (bottom == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight;
            }
            if (left == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight;
            }

            if (right == true)
            {
                fb[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = b[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight;
            }




        }
        ////////------------------------------------------------------------------------------------------------------------------------------------


        private void button_Closefrm_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

  


    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {


        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button_insertEIADetails_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button_ClearAll_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button_save_Click(object sender, RoutedEventArgs e)
        {

        }

        
    }
}
